package com.skt.manage.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.skt.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.skt.manage.domain.Medicalrecords;
import com.skt.manage.mapper.DoctorTreatmentsMapper;
import com.skt.manage.domain.DoctorTreatments;
import com.skt.manage.service.IDoctorTreatmentsService;

/**
 * 医生诊疗管理Service业务层处理
 * 
 * @author ffbaby
 * @date 2025-02-22
 */
@Service
public class DoctorTreatmentsServiceImpl implements IDoctorTreatmentsService 
{
    @Autowired
    private DoctorTreatmentsMapper doctorTreatmentsMapper;

    /**
     * 查询医生诊疗管理
     * 
     * @param treatmentId 医生诊疗管理主键
     * @return 医生诊疗管理
     */
    @Override
    public DoctorTreatments selectDoctorTreatmentsByTreatmentId(Long treatmentId)
    {
        return doctorTreatmentsMapper.selectDoctorTreatmentsByTreatmentId(treatmentId);
    }

    /**
     * 查询医生诊疗管理列表
     * 
     * @param doctorTreatments 医生诊疗管理
     * @return 医生诊疗管理
     */
    @Override
    public List<DoctorTreatments> selectDoctorTreatmentsList(DoctorTreatments doctorTreatments)
    {
        return doctorTreatmentsMapper.selectDoctorTreatmentsList(doctorTreatments);
    }

    /**
     * 新增医生诊疗管理
     * 
     * @param doctorTreatments 医生诊疗管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDoctorTreatments(DoctorTreatments doctorTreatments)
    {
        int rows = doctorTreatmentsMapper.insertDoctorTreatments(doctorTreatments);
        insertMedicalrecords(doctorTreatments);
        return rows;
    }

    /**
     * 修改医生诊疗管理
     * 
     * @param doctorTreatments 医生诊疗管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDoctorTreatments(DoctorTreatments doctorTreatments)
    {
        doctorTreatmentsMapper.deleteMedicalrecordsByRecordId(doctorTreatments.getTreatmentId());
        insertMedicalrecords(doctorTreatments);
        return doctorTreatmentsMapper.updateDoctorTreatments(doctorTreatments);
    }

    /**
     * 批量删除医生诊疗管理
     * 
     * @param treatmentIds 需要删除的医生诊疗管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDoctorTreatmentsByTreatmentIds(Long[] treatmentIds)
    {
        doctorTreatmentsMapper.deleteMedicalrecordsByRecordIds(treatmentIds);
        return doctorTreatmentsMapper.deleteDoctorTreatmentsByTreatmentIds(treatmentIds);
    }

    /**
     * 删除医生诊疗管理信息
     * 
     * @param treatmentId 医生诊疗管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDoctorTreatmentsByTreatmentId(Long treatmentId)
    {
        doctorTreatmentsMapper.deleteMedicalrecordsByRecordId(treatmentId);
        return doctorTreatmentsMapper.deleteDoctorTreatmentsByTreatmentId(treatmentId);
    }

    /**
     * 新增患者病历信息
     * 
     * @param doctorTreatments 医生诊疗管理对象
     */
    public void insertMedicalrecords(DoctorTreatments doctorTreatments)
    {
        List<Medicalrecords> medicalrecordsList = doctorTreatments.getMedicalrecordsList();
        Long treatmentId = doctorTreatments.getTreatmentId();
        if (StringUtils.isNotNull(medicalrecordsList))
        {
            List<Medicalrecords> list = new ArrayList<Medicalrecords>();
            for (Medicalrecords medicalrecords : medicalrecordsList)
            {
                medicalrecords.setRecordId(treatmentId);
                list.add(medicalrecords);
            }
            if (list.size() > 0)
            {
                doctorTreatmentsMapper.batchMedicalrecords(list);
            }
        }
    }
}
